<?php

/*
* Provides static function to print HTML elements,
* @author    René Giller
* @version   1.0
* @date      10.02.2009
*/
class HTML {

    /**
    * prints a link to promotion page if there is a promo
    */
    public static function printPromo() {
        global $model, $controller;

        if ($model->hasPromo())
            echo '<div id="promoBanner"><a href="'.$controller->getUrl('promo').'">'.$model->getLabel('promoBanner').'</a></div>';
    }
    
    /**
     * Creates a one level menu
     * @returns     String  the active top menu
     */
    public static function printTopMenu() {
        global $model, $controller;
        $topMenu = null;

        foreach (array_keys($model->menu) as $item) {
            if ($controller->isActive($item)) {
                if ($item == "home")  // because this page's name is on 1 line, so it can be vertically centered
                    $active = " active home";
                else
                    $active = " active";
                $topMenu = $item;
            } else {
                if ($item == "home")
                    $active = " home";
                else
                    $active = "";
            }
                            
            echo '<div class="box'.$active.'">';
            echo '<a class="'.$active.'" href="'.$controller->getUrl($item).'">'.$model->getLabel($item.'TopMenu').'</a>';
            echo '</div>';
            
        }
        return $topMenu;
    }
    
    /**
     * Creates a one level menu
     */
    /*public static function printTopMenu() {
        global $model, $controller;

        switch ($position) {
            case 'up' :
                foreach ($model->getMenuLevel1() as $level) {
                    if ($controller->isActive($level))
                        if ($level == "home")  // because the page is one 1 line, so it can be vertically centered
                            $active = " active home";
                        else
                            $active = " active";
                    else
                        if ($level == "home")
                            $active = " home";
                        else
                            $active = "";
                            
                    echo '<div class="box'.$active.'">';
                    echo '<a class="'.$active.'" href="'.$controller->getUrl($level).'">'.$model->getLabel($level).'</a>';
                    echo '</div>';
                }
                break;
                
            case 'left' :
                foreach ($model->getMenuLevel2() as $level) {
                    if ($controller->isActive($level))
                        echo '<div class="level active">'.$model->getLabel($level).'</div>';
                    else
                        echo '<div class="level"><a href="'.$controller->getUrl($level).'">'.$model->getLabel($level).'</a></div>';
                }
                break;
                
            default : 
                echo "Bad menu position :".$position;
        }
    }*/

    /**
    * creates a one levels menu
    */
    public static function printMenu() {
        global $model, $controller;
        
        foreach ($model->getSubMenu() as $key => $item) {
            echo '<div class="list">';
            echo '<div class="top">';
            
            if (!is_array($item)) { // level 1 without sub-levels
                if ($controller->isActive($item))
                    echo '<span id="active" >'.$model->getLabel($item.'Title').'</span>';
                else
                    echo '<a href="'.$controller->getUrl($item).'">'.$model->getLabel($item.'Title').'</a>';
                echo '</div>';   
            } else { // level 1 with sub-levels   
                echo '<a href="#" ';
                if ($controller->isActive($item))
                    echo 'class="active"';
                echo '>'.$model->getLabel($key.'Title').'</a>';
                echo '</div>';
        
                foreach ($item as $key => $submenu) {
                    if (!is_array($submenu)) { // level 2 without sub-levels  
                        $active = '';  
                        if ($controller->isActive($submenu))
                            echo '<div class="level"><span id="active">'.$model->getLabel($submenu.'Title').'</span></div>';                 
                        else
                            echo '<div class="level"><a href="'.$controller->getUrl($submenu).'">'.$model->getLabel($submenu.'Title').'</a></div>';                 
                    } else {  // level 2 with sub-levels 
                        $active = '';  
                        if ($controller->isActive($submenu))
                            $active = ' class="active" id="open"';
                        echo '<div class="level"><div><a'.$active.' href="#">'.$model->getLabel($key.'Title').'</a></div>';
                        foreach ($submenu as $thirdMenu) {
                            if ($controller->isActive($thirdMenu))
                                echo '<div class="level"><span id="active">'.$model->getLabel($thirdMenu.'Title').'</span></div>';
                            else
                                echo '<div class="level"><a href="'.$controller->getUrl($thirdMenu).'">'.$model->getLabel($thirdMenu.'Title').'</a></div>';
                        }
                        echo '</div>';
                    }
                }
            }
            echo '</div>';
        }
    }

    public static function printMenu1() {
        global $model, $controller;
        
        foreach ($model->getSubMenu() as $key => $item) {
            echo '<div class="list">';
            echo '<div class="top">';
            
            if (!is_array($item)) { // no sub-levels
                if ($controller->isActive($item))
                    echo '<span id="active" >'.$model->getLabel($item.'Title').'</span>';
                else
                    echo '<a href="'.$controller->getUrl($item).'">'.$model->getLabel($item.'Title').'</a>';
                echo '</div>';   
            } else { // level 1 with sub-levels   
                echo '<a href="#" ';
                if ($controller->isActive($item))
                    echo 'id="active"';
                echo '>'.$model->getLabel($key.'Title').'</a>';
                echo '</div>';
        
                foreach ($item as $key => $submenu) {
                    if (is_array($submenu)) { // level 2 with sub-levels
                        if ($controller->isActive($submenu))
                            echo '<div class="level active"><div><a href="#"><span class="active">'.$model->getLabel($key.'Title').'</span></a></div>';
                        else
                            echo '<div class="level"><div><a href="#">'.$model->getLabel($key.'Title').'</a></div>';
                        foreach ($submenu as $thirdMenu) {
                            if ($controller->isActive($submenu))
                                echo '<div class="level active"><span class="active">'.$model->getLabel($thirdMenu.'Title').'</span></div>';
                            else
                                echo '<div class="level"><a href="'.$controller->getUrl($thirdMenu).'">'.$model->getLabel($thirdMenu.'Title').'</a></div>';
                        }
                        echo '</div>';                     
                    } else { 
                    if ($controller->isActive($submenu))
                        echo '<div class="level active"><a href="#"><span id="active">'.$model->getLabel($submenu.'Title').'</span></a></div>';
                    else
                        echo '<div class="level"><a href="'.$controller->getUrl($submenu).'">'.$model->getLabel($submenu.'Title').'</a></div>';
                    }
                }
            }
            echo '</div>';
        }
    }
    
    /**
    * creates a two levels menu
    */
    public static function printMenu2() {
        global $model, $controller;

        foreach ($model->getSubMenu() as $level1 => $levels2) {
            if ($controller->isActive($levels2))
                $active = " active";
            else
                $active = "";

            echo '<div class="list">';
            echo '<div class="top'.$active.'"><a href="#">'.$model->getLabel($level1).'</a></div>';

            foreach ($levels2 as $level2) {
                if ($controller->isActive($level2))
                    echo '<div class="level active"><span id="active">'.$model->getLabel($level2).'</span></div>';
                else
                    echo '<div class="level"><a href="'.$controller->getUrl($level2).'">'.$model->getLabel($level2).'</a></div>';
            }
            echo '</div>';
        }
    }

    /**
    * prints all stored messages
    */
    public static function printMessages() {
        global $model, $controller;

        $messages = $controller->getMessages();

        if ($messages['success'] != "")
            echo '<div id="successMessage">'.$model->getLabel($messages['success']).'</div>';

        if (!empty($messages['error']))
            foreach ($messages['error'] as $type => $errorArray) {
                echo '<div class="errorMessage">';
                if ($type != 'send')
                    echo $model->getLabel($type);
                echo $model->getLabel($errorArray[0]);
                for ($i = 1; $i < count($errorArray); $i++)
                    echo ', '.$model->getLabel($errorArray[$i]);
                echo '</div>';
            }
    }

    /**
    * prints all ressources associated to the current page
    */
    public static function printRessources() {
        global $model;

        $content = $model->getContent();
        if (is_null($content))
            return;
        foreach ($content as $type) {
            $ressource = $model->get($type);
            if (is_null($ressource))
                return;
            self::printRessource($ressource, $type);
        }
    }

    /**
    * prints given ressource
    * @param ressource       mixed   ressource to be printed
    * @param type            String  ressource's type
    */
    private static function printRessource($ressource, $type) {
        global $model, $controller;
        
        switch ($type) {
            case 'page' :
                include 'site/view/'.$ressource.'.php';
                break;

            case 'title' :
                if ($model->currentPage == 'dab')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://www.radionumerique.ch/data_program_dab.aspx">www.radionumerique.ch</a></h1>';
                else if ($model->currentPage == 'tnt')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://www.radionumerique.ch/data_program_dvbt.aspx">www.radionumerique.ch</a></h1>';
                else if ($model->currentPage == 'map')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://broadcast.ch/data_programm_dvbs.aspx">www.broadcast.ch</a></h1>';
                else if ($model->currentPage == 'bistv')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://www.bis.tv/#chaines">www.bis.tv</a></h1>';
                else if ($model->currentPage == 'canalsat')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://www.canalsat.ch">www.canalsat.ch</a></h1>';
                else if ($model->currentPage == 'canalplus')
                    echo '<h1>'.$model->getLabel($ressource).' - <a href="http://www.canalplus.ch">www.canalplus.ch</a></h1>';
                else if ($model->currentPage == 'realization')
                    echo '<h1>Exemples de r&eacute;alisations</h1>';
                else
                    echo '<h1>'.$model->getLabel($ressource).'</h1>';
                if ($model->currentPage == 'canalsat')
                    echo '<h2>Offre de programmes</h2>';
                break;

            case 'randomImg' :
                echo '<div class="randImg">';
                echo '<a href="'.$ressource.'" type="extLink"><img src="'.$ressource.'" alt="'.$ressource.'"/></a>';
                echo '</div>';
                break;

            case 'map' :
                echo '<script type="text/javascript" src="http://map.search.ch/api/map.js"></script>';
                echo '<script type="text/javascript">new SearchChMap({center:"bulle/rte-de-riaz-3",x:"-3m",y:"1m",zoom:0.5});</script>';
                echo '<div id="mapcontainer"><noscript><p>';
                echo '<a href="http://map.search.ch/bulle/rte-de-riaz-3?x=-3m&amp;y=1m&amp;z=1024" type="extLink">';
                echo '<img src="http://map.search.ch/chmap.jpg?layer=bg,fg,copy,circle&amp;zd=0.5.5&amp;x=-3m&amp;y=1m&amp;w=560&amp;h=420&amp;poi=all&amp;base=1630+Bulle%2FRte+de+Riaz+3" alt="Rte de Riaz 3, Bulle"/>';
                echo '</a></p></noscript></div>';
                break;

            case 'parags' :
                $i = 0;
                foreach ($ressource as $text => $title) {
                    if (!empty($title))
                        echo '<h2>'.$model->getLabel($title).'</h2>';
                    if ($model->currentPage == 'contact' && $i++ == 2 )
                        echo '<div class="parag1">'.$model->getLinks($model->getLabel($text)).'</div>';
                    else
                        echo '<div class="parag">'.$model->getLinks($model->getLabel($text)).'</div>';
                }
                break;

            case 'timetable' :
                echo "<table>\n";
                echo '<tr><th></th><th>'.$model->getLabel('morning').'</th><th>'.$model->getLabel('afternoon').'</th></tr>';
                foreach ($ressource as $day => $time) {
                    echo '<tr>';
                    echo '<td class="left">'.$model->getLabel($day).'</td>';
                    if ($model->isLabel($time[0]))
                        echo '<td>'.$model->getLabel($time[0]).'</td>';
                    else
                        echo '<td>'.$time[0].'</td>';
                    if ($model->isLabel($time[1]))
                        echo '<td>'.$model->getLabel($time[1]).'</td>';
                    else
                        echo '<td>'.$time[1].'</td>';
                    echo '</tr>';
                }
                echo '</table>';
                break;

            case 'address' :
                echo '<div class="address">';
                foreach ($ressource as $line)
                    echo '<div class="line">'.$line.'</div>';
                echo '</div>';
                break;

            case 'imagesExtLink' :
                foreach ($ressource as $url => $image) {
                    echo '<div class="level">';
                    echo '<a href="'.$url.'" type="extLink"><img src="'.$image.'" alt="'.$image.'"/></a>';
                    echo '</div>';
                }
                break;

            case 'imagesExtLinkWithText' :
                foreach ($ressource as $img => $array) {
                    echo '<div class="level">';
                    echo '<a href="'.$img.'" type="extLink"><img src="'.$array['thumb'].'" alt="'.$array['thumb'].'"/></a>';
                    echo '<div>'.$array['text'].'</div>';
                    echo '</div>';
                }
                break;

            case 'imagesLink' :
                echo '<div class="clear">';
                foreach ($ressource as $brand => $image) {
                    echo '<div class="level">';
                    echo '<a href="'.$controller->getUrl('current', array('brand' => $brand)).'"><img src="'.$image.'" alt="'.$image.'"/></a>';
                    echo '</div>';
                }
                echo '</div>';
                break;

            case 'images' :
                echo '<div class="image">';
                foreach ($ressource as $image => $path) {
                    echo '<div class="level">';
                    echo '<img src="'.$path.'" alt="'.$image.'"/>';
                    echo '</div>';
                }
                echo '</div>';
                break;

            case 'offer' :
                echo '<h2>'.$ressource['title'].'</h2>';
                foreach ($ressource['links'] as $logo => $name) {
                    echo '<div class="row">';
                    echo '<img src="images/radio/'.$logo.'" alt="'.$name.'"/>';
                    echo '<div class="text">'.$name.'</div>';
                    echo '</div>';
                }
                break;

            case 'img' :
                foreach ($ressource as $name => $link ) {
                    echo '<div class="img">';
                    echo '<a href="'.$link.'" type="extLink"><img src="'.$link.'" alt="'.$name.'"/></a>';
                    echo '</div>';
                }
                break;

            case 'pdf' :
                echo '<div class="clear"><a href="'.$ressource.'" type="extLink">'.$model->getLabel('pdf').'</a></div>';
                break;

            case 'logo' :
                echo '<div class="logo"><a href="'.$ressource['link'].'" type="extLink"><img src="'.$ressource['logo'].'" alt="'.$ressource['link'].'"/></a></div>';
                break;

            case 'paragsImages' :
                foreach ($ressource as $array) {
                    echo '<div class="box">';
                    echo '<img src="'.$array['path'].'" alt="'.$array['image'].'"/>';
                    echo '<div class="text">';
                    echo '<h2>'.$model->getLabel($array['title']).'</h2>';
                    echo '<div class="parag">'.$model->getLabel($array['text']).'</div>';
                    echo '</div>';
                    echo '</div>';
                }
                break;
                
            case 'brand' :
                echo '<h2>'.$model->getLabel($ressource);
                echo ' - <a href="http://'.$model->brandLink[$ressource].'">'.$model->brandLink[$ressource].'</a></h2>';
                break;
                
            case 'promo' :
                echo '<h2>Actuellement en promotion</h2>';
                echo '<a href="index1.php?page=special">';
                echo '<img src="undefined" alt="promo" id="scrollImg"/>';
                echo '<script type="text/javascript">';
                echo 'var images = new Array();';
                echo 'var i = 0;';
                foreach ($ressource as $image)
                    echo 'images[i++] = "'.$image,'";';
                echo 'startSlideShow(images);';
                echo '</script>';
                echo '</a>';
                break;
                
            case 'activReal' :
                echo '<h2>Quelques r&eacute;alisations</h2>';
                echo '<a href="index1.php?page=cinema">';
                echo '<img src="undefined" alt="cinema" id="scrollImg"/>';
                echo '<script type="text/javascript">';
                echo 'var images = new Array();';
                echo 'var i = 0;';
                foreach ($ressource as $image)
                    echo 'images[i++] = "'.$image,'";';
                echo 'startSlideShow(images);';
                echo '</script>';
                echo '</a>';
                break;

            case 'articles' :
                foreach ($ressource as $article) {
                    echo '<div class="level">';
                    echo '<img src="'.$article->image.'" alt="'.$article->image.'"/>';
                    echo '<div class="box">';
                    echo '<div class="name">'.$article->name.'</div>';
                    if (!empty($article->text))
                        echo '<div class="text">'.$article->text.'</div>';
                    if (!empty($article->price_new))
                        echo '<div class="text">Prix neuf : CHF '.number_format($article->price_new, 2, '.', '\'').'</div>';
                    if (!empty($article->price))
                        echo '<div class="price">CHF '.number_format($article->price, 2, '.', '\'').'</div>';
                    echo '</div></div>';
                }
                break;

            case 'form' :
                echo '<form action="" method="post">';
                foreach ($ressource as $field => $data) {
                    switch($model->getFieldType($field)) {
                        case 'input' :
                            self::input($field);
                            break;

                        case 'textarea' :
                            self::textArea($field);
                            break;

                        default :
                            echo 'inexistant field type : '.$model->getFieldType();
                    }
                }
                echo '<div class="submit">';
                echo '<input type="submit" name="submit" value="'.$model->getLabel('send').'"/>';
                echo '<input type="reset" name="reset" value="'.$model->getLabel('reset').'"/>';
                echo '</div>';
                echo '</form>';
                break;

            case 'staff' :
                foreach ($ressource as $name => $data) {
                    echo '<div class="person">';
                    echo '<img src="'.$data['image'].'" alt="'.$data['image'].'"/>';
                    echo '<div class="name">'.$name.'</div>';
                    echo '<div class="job">'.$model->getLabel($data['job']).'</div>';
                    echo '</div>';
                }
                break;

            default :
                echo 'inexistant ressource type : '.$type;
        }
    }

    /**
    * creates an input type="text", sets the correct name and default value. Adds a * if field is required and
    * print expected format if available
    * @param field  String      field's name to be created
    */
    private static function input($field) {
        global $model;

        echo '<div class="input">';
        echo '<div class="label">'.$model->getLabel($field).'</div>';
        echo '<input type="text" name="'.$field.'" value="'.$model->getFieldValue($field).'"/>';
        if ($model->isRequired($field))
            echo '<div class="required">*</div>';
        if ($model->hasFormat($field))
            echo '<div class="format">'.$model->getFieldFormat($field).'</div>';
        echo '</div>';
    }

    /**
    * creates textarea, sets the correct name and default value
    * @param field  String      field's name to be created
    */
    private static function textArea($field) {
        global $model;

        echo '<div class="input">';
        echo '<div>'.$model->getLabel($field).'</div>';
        echo '<textarea cols="36" rows="10" name="'.$field.'">'.$model->getFieldValue($field).'</textarea>';
        echo '</div>';
    }
}